package com.lagou.edu.service.impl;

import com.lagou.edu.service.IMailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author 赵虎
 * @date 2021/2/23 9:57 上午
 */
@Slf4j
@Service
public class IMailServiceImpl implements IMailService {

    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String email;

    @Override
    public Boolean sendMailCode(String mail, String code) {
        // 构建一个邮件对象
        SimpleMailMessage message = new SimpleMailMessage();
        // 设置邮件主题
        message.setSubject("验证码");
        // 设置邮件发送者，这个跟application.yml中设置的要一致
        message.setFrom(email);
        // 设置邮件接收者，可以有多个接收者，中间用逗号隔开，以下类似
        message.setTo(mail);
        // 设置邮件发送日期
        message.setSentDate(new Date());
        // 设置邮件的正文
        message.setText(code);
        // 发送邮件
        javaMailSender.send(message);
        return true;
    }
}
